<?php

namespace App\Admin\Controllers;

use App\Admin\Actions\Grid\Detail;
use App\Admin\Repositories\Department;
use App\Models\Ding;
use App\Models\User;
use Dcat\Admin\Actions\Action;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Repositories\EloquentRepository;
use Dcat\Admin\Show;
use Illuminate\Http\Request;

class DepartmentController extends AdminController
{
    public bool $show_column_selector = false;
    public bool $create = false;
    public bool $edit = false;
    public string $delete_permission_name = 'department-delete';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid(): Grid
    {
        return Grid::make(new Department(), function (Grid $grid) {
            $grid->model()->with(['parent', 'ding']);
            $grid->column('id');
            $grid->column('ding_id')->display(function () {
                return $this->ding->name;
            });
            $grid->column('dept_parent_id')->display(function () {
                return $this->parent->name ?? '无';
            });
            $grid->column('dept_id');
            $grid->column('name');
            $grid->column('created_at');

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('ding_id')->select(Ding::query()->pluck('name', 'id'));
                $filter->like('name');
            });


            $grid->actions(function (Grid\Displayers\Actions $actions) {
                $actions->append(new Detail());
            });

            $this->gridCommon($grid);
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    public function detail($id): Show
    {
        return Show::make($id, new Department(), function (Show $show) {
            $show->model()->with(['parent', 'ding']);
            $show->field('id');
            $show->field('ding_id')->as(function () {
                return $this->ding->name;
            });
            $show->field('dept_parent_id')->as(function () {
                return $this->parent->name ?? '无';
            });;
            $show->field('dept_id');
            $show->field('name');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new Department(), function (Form $form) {
            $form->display('id');
            $form->text('ding_id');
            $form->text('dept_parent_id');
            $form->text('dept_id');
            $form->text('name');

            $form->display('created_at');
            $form->display('updated_at');
        });
    }


    public function destroy($id)
    {
        $department = \App\Models\Department::query()->findOrFail($id);
        if (User::query()->where('dept_id', $department->dept_id)->count()) {
            return $this->form()->response()->error('部门拥有员工，不可删除');
        }
        if (\App\Models\Department::query()->where('dept_parent_id', $department->dept_id)->count()) {
            return $this->form()->response()->error('部门拥有子部门，不可删除');
        }
        return $this->form()->destroy($id);
    }


    //获取部门数据
    public function getDept(Request $request): array
    {
        return \App\Models\Department::query()
            ->where('ding_id', $request->get('q', 0))
            ->select(['dept_id as id', 'name as text'])
            ->get()->toArray();
    }
}
